package com.ljc.controller;


import com.ljc.model.Admin;
import com.ljc.service.AdminService;
import com.ljc.utils.MapControl;
import com.google.common.base.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
public class loginController {
    @Autowired
    private AdminService adminService;
    @GetMapping("/login")
    public String v_login(){
        System.out.println("生成页面");

        return "adminlogin";
    }

    @PostMapping("/login")
    @ResponseBody
    public Map<String,Object> login(@RequestBody Map<String,Object> map, HttpServletRequest request){

        String username=map.get("username")+"";
        String password=map.get("password")+"";
        if(Strings.isNullOrEmpty(username)||Strings.isNullOrEmpty(password))
        {
            return MapControl.getInstance().error("用户名或密码不能为空").getMap();
        }
       Admin admin = adminService.login(username,password);
        if (admin!=null){
            HttpSession session = request.getSession();
            session.setAttribute("admin",admin);

            return MapControl.getInstance().success("登陆成功").getMap();
        }else
        {
            return MapControl.getInstance().error("用户名或密码cw").getMap();

        }

    }
}
